<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book Flask框架 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>从Hello World开始 | Flask框架</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../c02/s02.html" />
    
    
    <link rel="prev" href="../c02/index.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="2.1"
        data-chapter-title="从Hello World开始"
        data-filepath="c02/s01.md"
        data-basepath=".."
        data-revision="Sat Oct 28 2017 17:13:14 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        引言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="c01/index.html">
            
                
                    <a href="../c01/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        关于Flask
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="c01/s01.html">
            
                
                    <a href="../c01/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        了解框架
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="c01/s02.html">
            
                
                    <a href="../c01/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        安装环境
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="c02/index.html">
            
                
                    <a href="../c02/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="2.1" data-path="c02/s01.html">
            
                
                    <a href="../c02/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        从Hello World开始
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="c02/s02.html">
            
                
                    <a href="../c02/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        扩展
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="c03/index.html">
            
                
                    <a href="../c03/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="c03/s01.html">
            
                
                    <a href="../c03/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        变量
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="c03/s02.html">
            
                
                    <a href="../c03/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        过滤器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="c03/s03.html">
            
                
                    <a href="../c03/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        web表单
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="c03/s04.html">
            
                
                    <a href="../c03/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        控制语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="c03/s05.html">
            
                
                    <a href="../c03/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        宏、继承、包含
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="c03/s06.html">
            
                
                    <a href="../c03/s06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        Flask的特殊变量和方法
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="c04/index.html">
            
                
                    <a href="../c04/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="c04/s01.html">
            
                
                    <a href="../c04/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        数据库的设置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="c04/s02.html">
            
                
                    <a href="../c04/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        数据库的基本操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="c04/s03.html">
            
                
                    <a href="../c04/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        自定义模型类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="c04/s04.html">
            
                
                    <a href="../c04/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        数据库迁移
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="c04/s05.html">
            
                
                    <a href="../c04/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        邮件扩展
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="c05/index.html">
            
                
                    <a href="../c05/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        测试与部署
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="c05/s01.html">
            
                
                    <a href="../c05/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        蓝图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="c05/s02.html">
            
                
                    <a href="../c05/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        单元测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="c05/s03.html">
            
                
                    <a href="../c05/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        部署
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="c05/s04.html">
            
                
                    <a href="../c05/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        RESTful
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="c05/s05.html">
            
                
                    <a href="../c05/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        性能
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Flask框架</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="21-&#x4ECE;-hello-world-&#x5F00;&#x59CB;">2.1 &#x4ECE; Hello World &#x5F00;&#x59CB;</h1>
<h3 id="flask&#x7A0B;&#x5E8F;&#x8FD0;&#x884C;&#x8FC7;&#x7A0B;&#xFF1A;">Flask&#x7A0B;&#x5E8F;&#x8FD0;&#x884C;&#x8FC7;&#x7A0B;&#xFF1A;</h3>
<p> &#x6240;&#x6709;Flask&#x7A0B;&#x5E8F;&#x5FC5;&#x987B;&#x6709;&#x4E00;&#x4E2A;&#x7A0B;&#x5E8F;&#x5B9E;&#x4F8B;&#x3002;</p>
<p> Flask&#x8C03;&#x7528;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x540E;&#xFF0C;&#x4F1A;&#x5C06;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4F5C;&#x4E3A;&#x54CD;&#x5E94;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x3002;&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x54CD;&#x5E94;&#x5185;&#x5BB9;&#x4E3B;&#x8981;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x548C;&#x72B6;&#x6001;&#x7801;&#x3002;</p>
<p> &#x5F53;&#x5BA2;&#x6237;&#x7AEF;&#x60F3;&#x8981;&#x83B7;&#x53D6;&#x8D44;&#x6E90;&#x65F6;&#xFF0C;&#x4E00;&#x822C;&#x4F1A;&#x901A;&#x8FC7;&#x6D4F;&#x89C8;&#x5668;&#x53D1;&#x8D77;HTTP&#x8BF7;&#x6C42;&#x3002;&#x6B64;&#x65F6;&#xFF0C;Web&#x670D;&#x52A1;&#x5668;&#x4F7F;&#x7528;WSGI&#xFF08;Web Server Gateway Interface&#xFF09;&#x534F;&#x8BAE;&#xFF0C;&#x628A;&#x6765;&#x81EA;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x6240;&#x6709;&#x8BF7;&#x6C42;&#x90FD;&#x4EA4;&#x7ED9;Flask&#x7A0B;&#x5E8F;&#x5B9E;&#x4F8B;&#x3002;WSGI&#x662F;&#x4E3A; Python &#x8BED;&#x8A00;&#x5B9A;&#x4E49;&#x7684;Web&#x670D;&#x52A1;&#x5668;&#x548C;Web&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E4B;&#x95F4;&#x7684;&#x4E00;&#x79CD;&#x7B80;&#x5355;&#x800C;&#x901A;&#x7528;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x5B83;&#x5C01;&#x88C5;&#x4E86;&#x63A5;&#x53D7;HTTP&#x8BF7;&#x6C42;&#x3001;&#x89E3;&#x6790;HTTP&#x8BF7;&#x6C42;&#x3001;&#x53D1;&#x9001;HTTP&#xFF0C;&#x54CD;&#x5E94;&#x7B49;&#x7B49;&#x7684;&#x8FD9;&#x4E9B;&#x5E95;&#x5C42;&#x7684;&#x4EE3;&#x7801;&#x548C;&#x64CD;&#x4F5C;&#xFF0C;&#x4F7F;&#x5F00;&#x53D1;&#x8005;&#x53EF;&#x4EE5;&#x9AD8;&#x6548;&#x7684;&#x7F16;&#x5199;Web&#x5E94;&#x7528;&#x3002;</p>
<p> &#x7A0B;&#x5E8F;&#x5B9E;&#x4F8B;&#x4F7F;&#x7528;Werkzeug&#x6765;&#x505A;&#x8DEF;&#x7531;&#x5206;&#x53D1;&#xFF08;URL&#x8BF7;&#x6C42;&#x548C;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x4E4B;&#x95F4;&#x7684;&#x5BF9;&#x5E94;&#x5173;&#x7CFB;&#xFF09;&#x3002;&#x6839;&#x636E;&#x6BCF;&#x4E2A;URL&#x8BF7;&#x6C42;&#xFF0C;&#x627E;&#x5230;&#x5177;&#x4F53;&#x7684;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x3002;
 &#x5728;Flask&#x7A0B;&#x5E8F;&#x4E2D;&#xFF0C;&#x8DEF;&#x7531;&#x7684;&#x5B9E;&#x73B0;&#x4E00;&#x822C;&#x662F;&#x901A;&#x8FC7;&#x7A0B;&#x5E8F;&#x5B9E;&#x4F8B;&#x7684;route&#x88C5;&#x9970;&#x5668;&#x5B9E;&#x73B0;&#x3002;route&#x88C5;&#x9970;&#x5668;&#x5185;&#x90E8;&#x4F1A;&#x8C03;&#x7528;add_url_route()&#x65B9;&#x6CD5;&#x5B9E;&#x73B0;&#x8DEF;&#x7531;&#x6CE8;&#x518C;&#x3002;</p>
<p> &#x8C03;&#x7528;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#xFF0C;&#x83B7;&#x53D6;&#x54CD;&#x5E94;&#x6570;&#x636E;&#x540E;&#xFF0C;&#x628A;&#x6570;&#x636E;&#x4F20;&#x5165;HTML&#x6A21;&#x677F;&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x6A21;&#x677F;&#x5F15;&#x64CE;&#x8D1F;&#x8D23;&#x6E32;&#x67D3;&#x54CD;&#x5E94;&#x6570;&#x636E;&#xFF0C;&#x7136;&#x540E;&#x7531;Flask&#x8FD4;&#x56DE;&#x54CD;&#x5E94;&#x6570;&#x636E;&#x7ED9;&#x6D4F;&#x89C8;&#x5668;&#xFF0C;&#x6700;&#x540E;&#x6D4F;&#x89C8;&#x5668;&#x5904;&#x7406;&#x8FD4;&#x56DE;&#x7684;&#x7ED3;&#x679C;&#x663E;&#x793A;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x3002;</p>
<h2 id="&#x793A;&#x4F8B;&#xFF1A;">&#x793A;&#x4F8B;&#xFF1A;</h2>
<p>&#x65B0;&#x5EFA;&#x6587;&#x4EF6;hello.py:</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x5BFC;&#x5165;Flask&#x7C7B;</span>
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask

<span class="hljs-comment">#Flask&#x7C7B;&#x63A5;&#x6536;&#x4E00;&#x4E2A;&#x53C2;&#x6570;__name__</span>
app = Flask(__name__)

<span class="hljs-comment"># &#x88C5;&#x9970;&#x5668;&#x7684;&#x4F5C;&#x7528;&#x662F;&#x5C06;&#x8DEF;&#x7531;&#x6620;&#x5C04;&#x5230;&#x89C6;&#x56FE;&#x51FD;&#x6570;index</span>
<span class="hljs-decorator">@app.route(&apos;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;Hello World&apos;</span>

<span class="hljs-comment"># Flask&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x5B9E;&#x4F8B;&#x7684;run&#x65B9;&#x6CD5;&#x542F;&#x52A8;WEB&#x670D;&#x52A1;&#x5668;</span>
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    app.run()
</code></pre>
<h3 id="&#x67E5;&#x770B;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x4E2D;&#x7684;&#x8DEF;&#x7531;&#xFF1A;">&#x67E5;&#x770B;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x4E2D;&#x7684;&#x8DEF;&#x7531;&#xFF1A;</h3>
<p><img src="../images/&#x88C5;&#x9970;&#x5668;&#x8DEF;&#x7531;.png" alt="&#x88C5;&#x9970;&#x5668;&#x8DEF;&#x7531;"></p>
<h3 id="&#x7ED9;&#x8DEF;&#x7531;&#x4F20;&#x53C2;&#x793A;&#x4F8B;&#xFF1A;">&#x7ED9;&#x8DEF;&#x7531;&#x4F20;&#x53C2;&#x793A;&#x4F8B;&#xFF1A;</h3>
<p>&#x6709;&#x65F6;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5C06;&#x540C;&#x4E00;&#x7C7B;URL&#x6620;&#x5C04;&#x5230;&#x540C;&#x4E00;&#x4E2A;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x5904;&#x7406;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;&#x4F7F;&#x7528;&#x540C;&#x4E00;&#x4E2A;&#x89C6;&#x56FE;&#x51FD;&#x6570; &#x6765;&#x663E;&#x793A;&#x4E0D;&#x540C;&#x7528;&#x6237;&#x7684;&#x4E2A;&#x4EBA;&#x4FE1;&#x606F;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x8DEF;&#x7531;&#x4F20;&#x9012;&#x7684;&#x53C2;&#x6570;&#x9ED8;&#x8BA4;&#x5F53;&#x505A;string&#x5904;&#x7406;&#xFF0C;&#x8FD9;&#x91CC;&#x6307;&#x5B9A;int&#xFF0C;&#x5C16;&#x62EC;&#x53F7;&#x4E2D;&#x5192;&#x53F7;&#x540E;&#x9762;&#x7684;&#x5185;&#x5BB9;&#x662F;&#x52A8;&#x6001;&#x7684;</span>
<span class="hljs-decorator">@app.route(&apos;/user/&lt;int:id&gt;&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello_itcast</span><span class="hljs-params">(id)</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;hello itcast %d&apos;</span> %id
</code></pre>
<p><img src="../images/&#x52A8;&#x6001;&#x8DEF;&#x7531;.png" alt="&#x7ED9;&#x8DEF;&#x7531;&#x4F20;&#x53C2;"></p>
<h3 id="&#x8FD4;&#x56DE;&#x72B6;&#x6001;&#x7801;&#x793A;&#x4F8B;&#xFF1A;">&#x8FD4;&#x56DE;&#x72B6;&#x6001;&#x7801;&#x793A;&#x4F8B;&#xFF1A;</h3>
<p>return&#x540E;&#x9762;&#x53EF;&#x4EE5;&#x81EA;&#x4E3B;&#x5B9A;&#x4E49;&#x72B6;&#x6001;&#x7801;(&#x5373;&#x4F7F;&#x8FD9;&#x4E2A;&#x72B6;&#x6001;&#x7801;&#x4E0D;&#x5B58;&#x5728;)&#x3002;&#x5F53;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8BF7;&#x6C42;&#x5DF2;&#x7ECF;&#x5904;&#x7406;&#x5B8C;&#x6210;&#xFF0C;&#x7531;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x51B3;&#x5B9A;&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#x7801;&#xFF0C;&#x544A;&#x77E5;&#x5BA2;&#x6237;&#x7AEF;&#x8FD9;&#x6B21;&#x8BF7;&#x6C42;&#x7684;&#x5904;&#x7406;&#x7ED3;&#x679C;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-decorator">@app.route(&apos;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello_itcast</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;hello itcast&apos;</span>,<span class="hljs-number">999</span>
</code></pre>
<p><img src="../images/999.png" alt="999"></p>
<h3 id="abort&#x51FD;&#x6570;">abort&#x51FD;&#x6570;:</h3>
<p>&#x5982;&#x679C;&#x5728;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x51FA;&#x73B0;&#x4E86;&#x5F02;&#x5E38;&#x9519;&#x8BEF;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;abort&#x51FD;&#x6570;&#x7ACB;&#x5373;&#x7EC8;&#x6B62;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x7684;&#x6267;&#x884C;&#x3002;&#x901A;&#x8FC7;abort&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x5411;&#x524D;&#x7AEF;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;http&#x6807;&#x51C6;&#x4E2D;&#x5B58;&#x5728;&#x7684;&#x9519;&#x8BEF;&#x72B6;&#x6001;&#x7801;&#xFF0C;&#x8868;&#x793A;&#x51FA;&#x73B0;&#x7684;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x3002;</p>
<p>&#x4F7F;&#x7528;abort&#x629B;&#x51FA;&#x4E00;&#x4E2A;http&#x6807;&#x51C6;&#x4E2D;&#x4E0D;&#x5B58;&#x5728;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x72B6;&#x6001;&#x7801;&#xFF0C;&#x6CA1;&#x6709;&#x5B9E;&#x9645;&#x610F;&#x4E49;&#x3002;&#x5982;&#x679C;abort&#x51FD;&#x6570;&#x88AB;&#x89E6;&#x53D1;&#xFF0C;&#x5176;&#x540E;&#x9762;&#x7684;&#x8BED;&#x53E5;&#x5C06;&#x4E0D;&#x4F1A;&#x6267;&#x884C;&#x3002;&#x5176;&#x7C7B;&#x4F3C;&#x4E8E;python&#x4E2D;raise&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask,abort
<span class="hljs-decorator">@app.route(&apos;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello_itcast</span><span class="hljs-params">()</span>:</span>
    abort(<span class="hljs-number">404</span>)
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;hello itcast&apos;</span>,<span class="hljs-number">999</span>
</code></pre>
<h3 id="&#x6355;&#x83B7;&#x5F02;&#x5E38;&#xFF1A;">&#x6355;&#x83B7;&#x5F02;&#x5E38;&#xFF1A;</h3>
<p>&#x5728;Flask&#x4E2D;&#x901A;&#x8FC7;&#x88C5;&#x9970;&#x5668;&#x6765;&#x5B9E;&#x73B0;&#x6355;&#x83B7;&#x5F02;&#x5E38;&#xFF0C;errorhandler()&#x63A5;&#x6536;&#x7684;&#x53C2;&#x6570;&#x4E3A;&#x5F02;&#x5E38;&#x72B6;&#x6001;&#x7801;&#x3002;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x3002;</p>
<pre><code class="lang-python">
<span class="hljs-decorator">@app.errorhandler(404)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">error</span><span class="hljs-params">(e)</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;&#x60A8;&#x8BF7;&#x6C42;&#x7684;&#x9875;&#x9762;&#x4E0D;&#x5B58;&#x5728;&#x4E86;&#xFF0C;&#x8BF7;&#x786E;&#x8BA4;&#x540E;&#x518D;&#x6B21;&#x8BBF;&#x95EE;&#xFF01;%s&apos;</span>%e
</code></pre>
<p><img src="../images/&#x6355;&#x83B7;&#x5F02;&#x5E38;.png" alt="&#x6355;&#x83B7;&#x5F02;&#x5E38;"></p>
<h3 id="&#x91CD;&#x5B9A;&#x5411;redirect&#x793A;&#x4F8B;">&#x91CD;&#x5B9A;&#x5411;redirect&#x793A;&#x4F8B;</h3>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> redirect
<span class="hljs-decorator">@app.route(&apos;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello_itcast</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> redirect(<span class="hljs-string">&apos;http://www.itcast.cn&apos;</span>)
</code></pre>
<p><img src="../images/302.png" alt="302"></p>
<h3 id="&#x6B63;&#x5219;url&#x793A;&#x4F8B;&#xFF1A;">&#x6B63;&#x5219;URL&#x793A;&#x4F8B;&#xFF1A;</h3>
<p>&#x6B63;&#x5219;URL&#x662F;&#x4E3A;&#x4E86;&#x5339;&#x914D;&#x6307;&#x5B9A;&#x7684;URL&#xFF0C;&#x800C;&#x5339;&#x914D;&#x6307;&#x5B9A;&#x7684;URL&#x5219;&#x53EF;&#x4EE5;&#x8FBE;&#x5230;&#x9650;&#x5236;&#x8BBF;&#x95EE;&#xFF0C;&#x4EE5;&#x53CA;&#x4F18;&#x5316;&#x8BBF;&#x95EE;&#x8DEF;&#x5F84;&#x7684;&#x76EE;&#x7684;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask
<span class="hljs-keyword">from</span> werkzeug.routing <span class="hljs-keyword">import</span> BaseConverter

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Regex_url</span><span class="hljs-params">(BaseConverter)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,url_map,*args)</span>:</span>
        super(Regex_url,self).__init__(url_map)
        self.regex = args[<span class="hljs-number">0</span>]

app = Flask(__name__)
app.url_map.converters[<span class="hljs-string">&apos;re&apos;</span>] = Regex_url

<span class="hljs-decorator">@app.route(&apos;/user/&lt;re(&quot;[a-z]{3}&quot;):id&gt;&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello_itcast</span><span class="hljs-params">(id)</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;hello %s&apos;</span> %id
</code></pre>
<h3 id="&#x8BBE;&#x7F6E;cookie&#x548C;&#x83B7;&#x53D6;cookie">&#x8BBE;&#x7F6E;cookie&#x548C;&#x83B7;&#x53D6;cookie</h3>
<pre><code class="lang-python">
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask,make_response
<span class="hljs-decorator">@app.route(&apos;/cookie&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">set_cookie</span><span class="hljs-params">()</span>:</span>
    resp = make_response(<span class="hljs-string">&apos;this is to set cookie&apos;</span>)
    resp.set_cookie(<span class="hljs-string">&apos;username&apos;</span>, <span class="hljs-string">&apos;itcast&apos;</span>)
    <span class="hljs-keyword">return</span> resp
</code></pre>
<p><img src="../images/cookie.png" alt="&#x8BBE;&#x7F6E;cookie"></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask,request
<span class="hljs-comment">#&#x83B7;&#x53D6;cookie</span>
<span class="hljs-decorator">@app.route(&apos;/request&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">resp_cookie</span><span class="hljs-params">()</span>:</span>
    resp = request.cookies.get(<span class="hljs-string">&apos;username&apos;</span>)
    <span class="hljs-keyword">return</span> resp
</code></pre>
<p><img src="../images/&#x83B7;&#x53D6;cookie.png" alt="&#x83B7;&#x53D6;cookie"></p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../c02/index.html" class="navigation navigation-prev " aria-label="Previous page: 视图"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../c02/s02.html" class="navigation navigation-next " aria-label="Next page: 扩展"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book Flask框架 -->
</html>
